//
//  PerformanceTodayVM.swift
//  kxf
//
//  Created by Mac on 2024/3/24.
//

import Foundation
import UIKit

class PerformanceTodayVM: UIView {
    
    let selfHeight = kFitWidth(440)

    var tapBlock:(()->())?
    
    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: kFitWidth(1), y: frame.origin.y, width: kFitWidth(373), height: selfHeight))
        self.backgroundColor = .white
        self.isUserInteractionEnabled = true
        self.layer.cornerRadius = kFitWidth(8)
        self.clipsToBounds = true
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(tapAction))
        self.addGestureRecognizer(tap)
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    let titleVm : RegisterViewHeadTitleVM = {
        let vm = RegisterViewHeadTitleVM.init(frame: .zero)
        vm.titleLabel.text = "今日业绩"
        
        return vm
    }()
    lazy var bgImgView : UIImageView = {
        let img = UIImageView()
        img.setImgLocal(imgName: "performance_bg_img")
        img.isUserInteractionEnabled = true
        
        return img
    }()
    lazy var moneyLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .THEME
        lab.font = .systemFont(ofSize: 24, weight: .medium)
        lab.text = "0.00"
        lab.adjustsFontSizeToFitWidth = true
        
        return lab
    }()
    lazy var tipsLabel : UILabel = {
        let lab = UILabel()
        lab.text = "交易金额（元）"
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 14, weight: .medium)
        
        return lab
    }()
    lazy var tradeNumVm : PerformanceItemVM = {
        let vm = PerformanceItemVM.init(frame: CGRect.init(x: kFitWidth(13), y: kFitWidth(172), width: 0, height: 0))
        vm.titleLabel.text = "交易笔数"
        vm.numLabel.text = "0"
        return vm
    }()
    lazy var totalMercNumVm : PerformanceItemVM = {
        let vm = PerformanceItemVM.init(frame: CGRect.init(x: kFitWidth(194), y: kFitWidth(172), width: 0, height: 0))
        vm.titleLabel.text = "总商户数"
        vm.numLabel.text = "0"
        return vm
    }()
    lazy var rebateVm : PerformanceItemVM = {
        let vm = PerformanceItemVM.init(frame: CGRect.init(x: kFitWidth(13), y: kFitWidth(262), width: 0, height: 0))
        vm.titleLabel.text = "收益（元）"
        vm.numLabel.text = "0.00"
        return vm
    }()
    lazy var mercNumVm : PerformanceItemVM = {
        let vm = PerformanceItemVM.init(frame: CGRect.init(x: kFitWidth(194), y: kFitWidth(262), width: 0, height: 0))
        
        vm.titleLabel.text = "商户数"
        vm.numLabel.text = "0"
        return vm
    }()
    lazy var registMercNumVm : PerformanceItemVM = {
        let vm = PerformanceItemVM.init(frame: CGRect.init(x: kFitWidth(13), y: kFitWidth(352), width: 0, height: 0))
        
        vm.titleLabel.text = "注册商户数"
        vm.numLabel.text = "0"
        return vm
    }()
}

extension PerformanceTodayVM{
    func updateUI(dict:NSDictionary) {
        moneyLabel.text = "\(dict["todayTradeAmount"]as? String ?? "0.00")"
        tradeNumVm.numLabel.text = "\(dict["todayTradeCount"]as? String ?? "0")"
        totalMercNumVm.numLabel.text = "\(dict["totalMerchantNumber"]as? String ?? "")"
        rebateVm.numLabel.text = "\(dict["todayProfit"]as? String ?? "")"
        mercNumVm.numLabel.text = "\(dict["todayTradeUser"]as? String ?? "")"
        registMercNumVm.numLabel.text = "\(dict["merchantNumber"]as? String ?? "")"
    }
    @objc func tapAction() {
        if self.tapBlock != nil{
            self.tapBlock!()
        }
    }
}

extension PerformanceTodayVM{
    func initUI() {
        addSubview(titleVm)
        addSubview(bgImgView)
        bgImgView.addSubview(moneyLabel)
        bgImgView.addSubview(tipsLabel)
        
        addSubview(tradeNumVm)
        addSubview(totalMercNumVm)
        addSubview(rebateVm)
        addSubview(mercNumVm)
        addSubview(registMercNumVm)
        
        setConstrait()
    }
    func setConstrait() {
        bgImgView.snp.makeConstraints { make in
            make.centerX.lessThanOrEqualToSuperview()
            make.top.equalTo(kFitWidth(53))
            make.width.equalTo(kFitWidth(347))
            make.height.equalTo(kFitWidth(107))
        }
        moneyLabel.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(18))
            make.top.equalTo(kFitWidth(33))
            make.right.equalTo(kFitWidth(-20))
        }
        tipsLabel.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(18))
            make.top.equalTo(kFitWidth(69))
        }
    }
}
